//
//  WBVisitorView.swift
//  XLWebo
//
//  Created by 陈晓龙 on 17/4/5.
//  Copyright © 2017年 陈晓龙. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    //注册按钮
    lazy var registerButton:UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    //登录按钮
    lazy var loginButton:UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    
    
    
    //设置访客信息 dict [imageName:message]
    //首页
    //重写属性的didset方法计算值
    var visitorInfo:[String:String]? {
        didSet{
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            tipsLable.text = message
            
            if imageName == "" {
                startAnimation()
                return
            }
            //其他控制器中不需要遮罩和小小房子
            iconView.image = UIImage(named: imageName)
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //圆圈旋转
    func startAnimation() {
        
        /*
         
         CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@“transform.rotation.z"];
         //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
         animation.fromValue = [NSNumbernumberWithFloat:0.f];
         animation.toValue =  [NSNumbernumberWithFloat: M_PI *2];
         animation.duration  = 3;
         animation.autoreverses = NO;
         animation.fillMode =kCAFillModeForwards;
         animation.repeatCount = 500;
         [view.layer addAnimation:animation2 forKey:nil];
         */
        
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.fromValue = 0
        animation.toValue = M_PI*2
        animation.duration = 15
        animation.autoreverses = false
        animation.fillMode = kCAFillModeForwards
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false//完成之后不删除
        iconView.layer.add(animation, forKey: nil)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
 
//MARK: -- 私有控件
    //圆圈
     lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
     lazy var houseIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示标签
     lazy var tipsLable:UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
   
}

extension WBVisitorView {
    func setupUI() {
        backgroundColor = UIColor.cz_color(withRed: 237, green: 237, blue: 237)
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipsLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        tipsLable.textAlignment = .center
        //使用自动布局 要取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //1.自动布局 --圆圈布局
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        //2、 小房子布局
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //提示标签
        addConstraint(NSLayoutConstraint(item: tipsLable,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipsLable,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20.0))
        addConstraint(NSLayoutConstraint(item: tipsLable,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant:236))
        //注册按钮
        //addConstraint(NSLayoutConstraint(item: registerButton, attribute: .centerX, relatedBy: .equal, toItem: tipsLable, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton ,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipsLable,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton ,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipsLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20.0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100.0))
    //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton ,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipsLable,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton ,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100.0))
   
    //遮罩的位置
        let viewDict = ["maskIconView":maskIconView,
                        "registerButton":registerButton] as [String : Any]
        let metrics = ["spacing":-35]
        
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: viewDict))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registerButton]", options: [], metrics: metrics, views: viewDict))
    
    }
    
    
    
    
}







